package com.merlin.gateway;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;


/**
 * 执行请求前的过滤器
 * @author itw_huomb
 * @date 2016年7月26日
 */
public class PreFilter extends ZuulFilter
{

	private static Logger log = LoggerFactory.getLogger(PreFilter.class);

	@Override
	public String filterType()
	{
		return "pre";
	}

	@Override
	public int filterOrder()
	{
		return 1;
	}

	@Override
	public boolean shouldFilter()
	{
		return true;
	}

	/**
	 * 把每次访问的url记录到日志里。
	 * 把带权限的头加到zuul代理头里
	 */
	@Override
	public Object run()
	{
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();

		ctx.addZuulRequestHeader("Authorization", request.getHeader("Authorization"));

		log.info(String.format("%s 请求地址为  %s", request.getMethod(), request.getRequestURL().toString()));
		return null;
	}

}
